/** 
 * @Title: CarInfoByJson.java 
 * @Package com.zizo.autohome.carinfo.crawl 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月7日 上午10:39:52 
 */
package com.zizo.autohome.carinfo.crawl;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zizo.autohome.carinfo.bean.CarInfo;
import com.zizo.autohome.carinfo.bean.ModelInfo;
import com.zizo.autohome.carinfo.dao.CarInfoDaoOfAutoHome;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;

/**
 * @ClassName: CarInfoByJson
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月7日 上午10:39:52
 * 
 */
public class CarInfoCrawlByJson {
	private CarInfoDaoOfAutoHome dao = new CarInfoDaoOfAutoHome(ConnectionPool.getConnection());

	public void brand() {
		String url = "http://www.autohome.com.cn/ashx/AjaxIndexCarFind.ashx?type=1";
		// String url =
		// "http://k.autohome.com.cn/proxy/brand?state=0X001C&typeid=0&radom=1473215448343";
		String doc = DocUtil.getJsonDataFromUrl(url);
		String body = doc.split("\"result\":")[1];
		String json = body.substring(0, body.length() - 1);
		try {
			JSONObject jsonObj = new JSONObject(json);
			JSONArray jsonArr = jsonObj.getJSONArray("branditems");
			for (int i = 0; i < jsonArr.length(); i++) {
				JSONObject brandObj = jsonArr.getJSONObject(i);
				int brand_id = brandObj.getInt("id");
				String brand_name = brandObj.getString("name");
				String brand_bfirstletter = brandObj.getString("bfirstletter");
				car(brand_id, brand_name, brand_bfirstletter);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void car(int brand_id, String brand_name, String brand_bfirstletter) {
		// url =
		// http://www.autohome.com.cn/ashx/AjaxIndexCarFind.ashx?type=3&value=33
		String url = "http://www.autohome.com.cn/ashx/AjaxIndexCarFind.ashx?type=3&value=" + brand_id;
		String doc = DocUtil.getJsonDataFromUrl(url);
		String body = doc.split("\"result\":")[1];
		String json = body.substring(0, body.length() - 1);
		try {
			JSONObject jsonObj = new JSONObject(json);
			JSONArray jsonArr = jsonObj.getJSONArray("factoryitems");
			for (int i = 0; i < jsonArr.length(); i++) {
				JSONObject conObj = jsonArr.getJSONObject(i);
				int oem_id = conObj.getInt("id");
				String oem_name = conObj.getString("name");
				String oem_firstletter = conObj.getString("firstletter");
				JSONArray conArr = conObj.getJSONArray("seriesitems");
				for (int j = 0; j < conArr.length(); j++) {
					JSONObject modelObj = conArr.getJSONObject(j);
					int car_id = modelObj.getInt("id");
					String car_name = modelObj.getString("name");
					String car_firstletter = modelObj.getString("firstletter");
					CarInfo carinfo = new CarInfo(brand_id, brand_name, brand_bfirstletter, oem_id, oem_name,
							oem_firstletter, car_id, car_name, car_firstletter);
					dao.insertData(carinfo);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void model(int carid) {
		// url =
		// http://www.autohome.com.cn/ashx/AjaxIndexCarFind.ashx?type=5&value=812;
		String url = "http://www.autohome.com.cn/ashx/AjaxIndexCarFind.ashx?type=5&value=" + carid;
		String doc = DocUtil.getJsonDataFromUrl(url);
		String body = doc.split("\"result\":")[1];
		String json = body.substring(0, body.length() - 1);
		try {
			JSONObject jsonObj = new JSONObject(json);
			JSONArray jsonArr = jsonObj.getJSONArray("yearitems");
			for (int i = 0; i < jsonArr.length(); i++) {
				JSONObject yearObj = jsonArr.getJSONObject(i);
				int year_id = yearObj.getInt("id");
				String year_name = yearObj.getString("name");
				JSONArray yearArr = yearObj.getJSONArray("specitems");
				for (int j = 0; j < yearArr.length(); j++) {
					JSONObject modelObj = yearArr.getJSONObject(j);
					int model_id = modelObj.getInt("id");
					String model_name = modelObj.getString("name");
					double model_minprice = modelObj.getDouble("minprice");
					double model_maxprice = modelObj.getDouble("maxprice");
					ModelInfo modelInfo = new ModelInfo(carid, year_id, year_name, model_id, model_name, model_minprice,
							model_maxprice);
					System.out.println(modelInfo);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		CarInfoCrawlByJson car = new CarInfoCrawlByJson();
		car.brand();
	}
}
